package gbench.sandbox.tree;

import org.junit.jupiter.api.Test;

import gbench.common.fs.FileSystem;
import gbench.common.fs.XlsFile.SimpleExcel;
import gbench.common.tree.LittleTree;
import gbench.common.tree.LittleTree.IRecord;

import static gbench.common.tree.LittleTree.IRecord.L;
import static gbench.common.tree.LittleTree.IRecord.REC;
import static gbench.common.tree.LittleTree.IRecord.llclc;
import gbench.common.tree.LittleTree.Node;

/**
 * 策略树
 * @author gbench
 *
 */
public class JunitStrategyTree {
    
    /**
     * 对 strategy_tree 的解析：博弈树的数据处理
     */
    @Test
    public void strategy_tree() {
        final var excel= new SimpleExcel();
        final var path = "./tree.xlsx";
        excel.loadXlsx(FileSystem.stream(FileSystem.path(path, this.getClass())));
        final Node<IRecord> wbsRoot = excel.autoDetect("wbs") // 节点结构:Id WbsCode Activity Successor Duration Resource
            .mapByRow(IRecord::REC).collect( llclc( wbsItems -> LittleTree.buildTree( wbsItems, // 节点集合
                wbsItems.get(wbsItems.size()-1).get("Id"),  // 根节点
                REC("id", "Id", "pid", "Successor"), // 树形结构的字段名称关系
                node->node.setLambdaName(rec->rec.getValue(r->r.str("Activity"))) // 更改名称函数:提取Activity字段作为节点名称
            )));// 生成属性结构
        
        wbsRoot.forEach(node->{
            System.out.println("\t".repeat(node.getLevel()-1)+node.getPathCode()+"/"+node.getName());
        });// forEach
        
        excel.close();
    }
    
    @Test
    public void bar() {
        
        final var hierachies = L( // 层级结构数据 id name pid
            "0   中国          -",

            "1   北京          0", 
            "1.1   东城区          1",
            "1.2   朝阳区          1",

            "2   上海          0", 
            "2.1   长宁区          2",
            "2.2   徐汇区          2",

            "3   天津          0", 
            "4   重庆          0");

        // id name pid 列表
        final var builder = IRecord.builder("id,name,pid");
        final var tree = hierachies.stream().map(line -> line.split("\\s+")).map(builder::get)
            .collect(llclc(LittleTree::buildTree));

        tree.forEach(e -> {
            System.out.println("\t".repeat(e.getLevel()-1) + e);
        });
    }
}
